home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / shells / rc-1.000 / rc-1 / rc-1.5-linux / b_seq.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-22  |  1.2 KB  |  43 lines

  1. #include "addon.h"
  2. typedef enum bool { FALSE, TRUE } bool;
  3. extern void set(bool);
  4. #include <stdio.h>   /* need for printf() */
  5. #include <stdlib.h>  /* need for atio() */
  6. /*
  7. ANSI C program to output a sequence of integers:  seq <n1> <n2> [step]
  8. outputs the integers n1 .. n2 with an optional positive *or* negative step size.
  9. Note that the use of sign in the for loop condition ensures proper termination.
  10. RETURN VALUES: 0 -OK, 1 -invalid step, 2 step/order mismatch.
  11. Modified to be an rc builtin.            Chuck Blake, 03-14-1994 */
  12.  
  13. void b_seq(char* argv[]) {
  14.     int i, argc=0, arg1, arg2, step, sign;
  15.  
  16. /* argument processing */
  17.     while (argv[argc] != 0) ++argc;
  18.  
  19.     if (argc<3 || argc>4) {
  20.         printf("seq n1 n2 [step] outputs the sequence (n1..n2 by step)\n");
  21.         set(FALSE);
  22.         return;
  23.     }
  24.     arg1 = atoi(argv[1]);
  25.     arg2 = atoi(argv[2]);
  26.     step = (argc==4) ? atoi(argv[3]) : 1;
  27.     if (step==0) {
  28.         set(FALSE);
  29.         return;
  30.     }
  31.     if (step>0 && arg1>arg2) {
  32.         set(FALSE);
  33.         return;
  34.     }
  35.  
  36. /* main logic */
  37.     sign = (step>0) ? 1 : -1;
  38.     for(i=arg1; sign*i <= sign*arg2; i+=step)
  39.         printf("%i ",i);
  40.     set(TRUE);
  41.     return;
  42. }
  43.